package com.yaozheng.vocationaltraining.service.impl.exam;

import com.yaozheng.vocationaltraining.iview.BaseView;
import com.yaozheng.vocationaltraining.iview.IAddQuestionsView;
import com.yaozheng.vocationaltraining.iview.IPKRankView;
import com.yaozheng.vocationaltraining.service.exam.PKRankService;
import com.yaozheng.vocationaltraining.utils.Constants;
import com.yaozheng.vocationaltraining.utils.ErrorUtils;
import com.yaozheng.vocationaltraining.utils.http.okhttp.OkHttpClientManager;

import net.sf.json.JSONObject;

import org.androidannotations.annotations.EBean;

/**
 * Created by moon on 2017/11/14.
 */

@EBean
public class PKRankServiceImpl implements PKRankService {
    IPKRankView iTokenBaseView;
    @Override
    public void init(IPKRankView iTokenBaseView) {
        this.iTokenBaseView = iTokenBaseView;
    }

    @Override
    public void loadMyRank() {
        try {
            OkHttpClientManager
                    .getInstance()
                    .get(Constants.ROOTURL + "/pk/my-rank",
                            iTokenBaseView.getToken(), new BaseView(iTokenBaseView) {
                                @Override
                                public void responseSuccess(JSONObject jsonObject) {
                                    iTokenBaseView.myRankSuccess(jsonObject);
                                }

                                @Override
                                public void responseError(String error) {
                                    iTokenBaseView.myRankError(error);
                                }
                            });
        } catch (Exception e) {
            ErrorUtils.outErrorLog(e);
            if (iTokenBaseView.isResponseResult()) {
                iTokenBaseView
                        .myRankError(ErrorUtils.SERVER_CONNECTION_ERROR);
            }

        }
    }

    @Override
    public void list() {
        try {
            OkHttpClientManager
                    .getInstance()
                    .get(Constants.ROOTURL + "/pk/rank",
                            iTokenBaseView.getToken(), new BaseView(iTokenBaseView) {
                                @Override
                                public void responseSuccess(JSONObject jsonObject) {
                                    iTokenBaseView.responseSuccess(jsonObject);
                                }

                                @Override
                                public void responseError(String error) {
                                    iTokenBaseView.responseError(error);
                                }
                            });
        } catch (Exception e) {
            ErrorUtils.outErrorLog(e);
            if (iTokenBaseView.isResponseResult()) {
                iTokenBaseView
                        .responseError(ErrorUtils.SERVER_CONNECTION_ERROR);
            }

        }
    }
}
